package com.nmh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nmh.config.R;
import com.nmh.entity.City;
import com.nmh.entity.CityOtherOne;
import com.nmh.mapper.cityMapper;
import com.nmh.service.cityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags="省市区")
@RestController
@Slf4j
@RequestMapping("/city")
public class cityController {
    @Autowired
    private cityService cityService;
    @Autowired
    private cityMapper cityMapper;
    /*
    获取省份
     */
    @ApiOperation(value="获取省市区接口")
    @GetMapping("/sheng")
    public R<List<CityOtherOne>> getSheng(){
        LambdaQueryWrapper<City> wrapper=new LambdaQueryWrapper<City>()
                .like(City::getLevel,1);
        List<City> shengList=cityService.list(wrapper);
        //获取省
        List<CityOtherOne> oneList=new ArrayList<>();
        for(City c:shengList){
            CityOtherOne cityOtherOne=new CityOtherOne();
            BeanUtils.copyProperties(c,cityOtherOne);
            oneList.add(cityOtherOne);
        }
        //获取市
        for(CityOtherOne c:oneList){
            LambdaQueryWrapper<City> wrapper1=new LambdaQueryWrapper<City>()
                    .eq(City::getParentId,c.getId());
            List<City> CityList=cityService.list(wrapper1);
            List<CityOtherOne> City=new ArrayList<>();
            for(City cc:CityList){
                CityOtherOne cityOtherOne=new CityOtherOne();
                BeanUtils.copyProperties(cc,cityOtherOne);
                City.add(cityOtherOne);
                //获取区
                LambdaQueryWrapper<City> wrapper2=new LambdaQueryWrapper<City>()
                        .eq(com.nmh.entity.City::getParentId,cityOtherOne.getId());
                List<City> TownList=cityService.list(wrapper2);
                List<CityOtherOne> Town=new ArrayList<>();
                for(City ccc:TownList){
                    CityOtherOne cityOtherOne1=new CityOtherOne();
                    BeanUtils.copyProperties(ccc,cityOtherOne1);
                    Town.add(cityOtherOne1);
                }
                cityOtherOne.setChild(Town);
            }
            c.setChild(City);
        }
       return R.success(oneList);
    }
}
